home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / sd22.arc / SD22.ASM next >
Assembly Source File  |  1987-05-21  |  16KB  |  427 lines

  1. ;------------------------------------------------------------------------------;
  2. ;                                                                              ;
  3. ;                                                                              ;
  4. ;                                                                              ;
  5. ; Switch Directory - SD                                                        ;
  6. ;                                                                              ;
  7. ;                                                                              ;
  8. ;                                                                              ;
  9. ;Usage                                                                         ;
  10. ;                                                                              ;
  11. ;   [d:\....]>SD [drive][switch][subdirectory name]                            ;
  12. ;                                                                              ;
  13. ;                        [drive] - the drive to search (if not searhing        ;
  14. ;                                  current drive)                              ;
  15. ;                                                                              ;
  16. ;                        [switch] = blank - search whole disk                  ;
  17. ;                                                                              ;
  18. ;                                 = \ - search only subdirectories of the      ;
  19. ;                                       current directory (if no other \       ;
  20. ;                                       in subdirectory name)                  ;
  21. ;                                                                              ;
  22. ;                                 = / - search only one path deep. Searches    ;
  23. ;                                       the root directory only. (Similar      ;
  24. ;                                       to DOS CD command with one pathname)   ;
  25. ;                                                                              ;
  26. ;                                                                              ;
  27. ;                                                                              ;
  28. ;                        [subdirectory name] - the name of an individual       ;
  29. ;                                              subdirectory or a complete path ;
  30. ;                                                                              ;
  31. ;                        If the command line is left blank then you are        ;
  32. ;                        taken to the root directory of the disk               ;
  33. ;                                                                              ;
  34. ;                                                                              ;
  35. ;Sorce code heavily dependent on Vern Buerg's LDIR program. Thanks Vern        ;
  36. ;                                                                              ;
  37. ;3/11/87 - Stephen Falatko                                                     ;
  38. ;                                                                              ;
  39. ;Written for the A86 assembler                                                 ;
  40. ;                                                                              ;
  41. ;------------------------------------------------------------------------------;
  42.       ;    Simple change directory
  43.  
  44. CODE SEGMENT
  45.  
  46.       Org    0100
  47.  
  48. Stackx       Dw      0                       ;Entry stack pointer
  49.  
  50.       Mov    Stackx,SP               ;Save stack ptr for exiting
  51.       Jmp    Start
  52. ;
  53. ;     Data Areas, Constants, Etc.
  54.  
  55. LF           Equ     10
  56. CR           Equ     13
  57. Stopper      Equ     255                    ;Ends print strings
  58.  
  59. CURDSK       Equ     019                     ;Get current disk
  60. SETDTA       Equ     01A                     ;Set data transfer area
  61. CHDIR        Equ     03B                     ;Change directory
  62. GETPATH      Equ     047                     ;Get current directory
  63.  
  64. Errlvl       Db      0                       ;DOS return code
  65.  
  66. RootFlag     Db      0
  67. CDFlag       Db      0
  68. OneDeepFlag  Db      0
  69.  
  70. Sub_DirStart Db      'x:\'
  71. Sub_Dir      Db      64 Dup (0)              ;The sub dir we want to change to
  72.  
  73. Done_Flag    Dw      0
  74.  
  75. Count        Dw      0                       ;Number of args on command line
  76.  
  77. DtaPointer   Dw      DtaAreaBegin
  78. Direction    Db      0
  79. BackOneDir   Db      '..',0
  80. SearchAsciiZ Db      '*.*',0
  81.  
  82. ;
  83. ;     Headings and titles
  84.  
  85. Not_Found_Msg  Db  CR,LF,'Subdirectory Not Found',CR,LF,Stopper
  86.  
  87. Help           Db  CR,LF,'Usage:',CR,LF,CR,LF
  88.                Db  '[d:\....]>SD [drive][switch][subdirectory name]',CR,LF
  89.                Db  '      [drive] - the drive to search. (if not searching',CR,LF
  90.                Db  '                current drive)',CR,LF,CR,LF
  91.                Db  '      [switch] = blank - search whole disk',CR,LF,CR,LF
  92.                Db  '               = \ - search only subdirectories of the',CR,LF
  93.                Db  '                     current directory (if no other',CR,LF
  94.                Db  '                     \ in subdirectory name',CR,LF,CR,LF
  95.                Db  '               = / - search only one path deep. Searches',CR,LF
  96.                Db  '                     the root directory only. (Similar',CR,LF
  97.                Db  '                     to DOS CD command with one pathname)',CR,LF,CR,LF
  98.                Db  '      [subdirectory name] - the name of an individual',CR,LF
  99.                Db  '                            subdirectory or complete path',CR,LF,CR,LF
  100.                Db  '      If the command line is left blank then you are',CR,LF
  101.                Db  '      taken to the root directory of the disk',CR,LF,Stopper
  102.  
  103. OrigDr       Db      'x:'                    ;Original drive
  104. OrigDir      Db      '\',63 Dup (0)          ; and path
  105.  
  106. RootDir      Db      'x:\',0                 ;To get vol label
  107.  
  108.  
  109. ;
  110. ;     Set default drive and path
  111.  
  112. Start:
  113.       Mov    AH,0D                   ; Reset diskettes
  114.       Int    021
  115.  
  116.       Mov    AH,CURDSK               ; Get current disk
  117.       Int    021
  118.       Add    AL,'A'
  119.       Mov    OrigDr,AL               ; Save original drive letter
  120.       Mov    RootDir,AL              ;
  121.  
  122.       Mov    AH,GETPATH              ; Save original path
  123.       Mov    DL,OrigDr
  124.       Sub    DL,'@'                  ; a little monkey business to set
  125.       Mov    SI,Offset OrigDir + 1   ; the original drive
  126.       Int    021
  127.  
  128.       Mov    AX,02523                ; set Ctrl+Break vector to point
  129.       Mov    DX,Offset NFCB          ; to our not found. This way a Ctrl+Brk
  130.       Int    021                     ; will leave us in the place we started
  131.  
  132.       Sub    CX,CX                   ; Clear CX
  133.       Mov    CL,B [080]              ; Get the number of characters in
  134.  
  135.       Cmp    CX,0                    ; Anything?
  136.       Jne    L1
  137.       Call No_Arg                    ;set root dir and leave
  138.       Jmp    Exit
  139.   L1:
  140.       Mov    SI,081                  ; set up to parse command line
  141.       Mov    DI,Offset Sub_dir
  142.  
  143.       Push   DI
  144.       Mov    DI,SI
  145.       Mov    al,':'                  ;drive specifier present?
  146.       Repne  Scasb
  147.       Cmp    B [DI-1],':'
  148.       Jne    L10
  149.       Mov    SI,DI                   ;point SI to character following :
  150.       Sub    DI,2                    ;point DI to drive letter
  151.       Mov    AL,B [DI]
  152.       Cmp    AL,'a'
  153.       If ge Xor AL,020
  154.       Mov    RootDir,AL
  155.  
  156.       Sub    DX,DX
  157.       Mov    AH,0E                   ; set drive
  158.       Mov    DL,AL
  159.       Sub    DL,'A'
  160.       Int    021
  161.  
  162.       Cmp    B [SI],CR
  163.       If e Call No_Arg
  164.  
  165.       Pop    DI
  166.       Jmp Short L2
  167.   L10:
  168.       Pop    DI
  169.   L2:
  170.       Lodsb
  171.  
  172.       Cmp    AL,' '                  ;strip leading blanks
  173.       Jz     L2
  174.  
  175.       Cmp    AL,0D                   ;carriage return
  176.       Jz     L7
  177.  
  178.       Cmp    AL,'?'                  ;?
  179.       Jne    L3
  180.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  181.       Jne    L3
  182.       Mov    DX,Offset Help
  183.       Call PrintS
  184.       Jmp    Exit                    ;load next
  185.   L3:
  186.       Cmp    AL,'/'                  ;/ ?
  187.       Jne    L30
  188.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  189.       Jne    L30
  190.       Mov    OneDeepFlag,1           ;set flag so we only search one deep
  191.       Jmp Short L2                   ;load next
  192.   L30:
  193.       Cmp    AL,'\'                  ;\ ?
  194.       Jne    L4
  195.       Cmp    DI,Offset Sub_Dir       ;still pointing to beginning?
  196.       Jne    L31
  197.       Mov    RootFlag,1              ;set flag so we won't set to root
  198.       Jmp Short L2                   ;load next
  199.   L31:
  200.       Cmp    CDFlag,1
  201.       Je     L32
  202.       Mov    CDFlag,1                ;must be full path specifier
  203.       Push   ax
  204.       Mov    al,RootDir
  205.       Mov    Sub_DirStart,al
  206.       Pop    ax
  207.   L32:
  208.       Stosb                          ;load it up
  209.       Jmp Short L2                   ;and get next char
  210.   L4:
  211.       Cmp    RootFlag,1              ;been here before?
  212.       Je     L5                      ;yep, go on
  213.       Push   AX
  214.       Call No_Arg                    ;no, set to root directory
  215.       Pop    Ax
  216.       Mov    RootFlag,1              ;set flag
  217.   L5:
  218.       Cmp    AL,'0'                  ;compare with 0
  219.       Jb     L2                      ;get next char if smaller
  220.  
  221.       Cmp    AL,'z'                  ;compare with z
  222.       Ja     L2                      ;get next char if bigger
  223.  
  224.       Cmp    AL,'a'                  ;lowercase letter?
  225.       Jb     L6                      ;nope so go on
  226.       Xor    AL,020                  ;make upper case
  227.   L6:
  228.       Stosb
  229.       Jmp Short L2
  230.   L7:
  231.       Mov    Count,DI                ;how many characters stored?
  232.       Sub    Count,Offset Sub_Dir
  233.       Cmp    Count,0
  234.       Je     Exit
  235.  
  236.       Mov    AL,0
  237.       Stosb
  238.  
  239.       Cmp    CDFlag,1
  240.       Je     SetPath
  241.  
  242.       Call   GetDir                  ; Read the directory
  243.  
  244.       Cmp    Done_Flag,1
  245.       Jne    Not_Found
  246.  
  247.       Jmp Short Exit
  248.  
  249.  
  250. Not_Found:
  251.  
  252.       Mov    DX,Offset Not_Found_Msg
  253.       Call   PrintS
  254.   NFCB:
  255.       Sub    DX,DX
  256.       Mov    DL,OrigDr
  257.       Cmp    DL,RootDir
  258.       Je     NF1
  259.  
  260.       Mov    AH,0E                   ; set drive
  261.       Sub    DL,'A'
  262.       Int    021
  263.    NF1:
  264.       Mov    AH,CHDIR                ;Set path to original path
  265.       Mov    DX,Offset OrigDr
  266.       Int    021
  267.  
  268.  
  269.       Jmp Short Exit
  270.  
  271. SetPath:
  272.       Mov    AH,CHDIR
  273.       Mov    DX,Offset SUb_DirStart
  274.       Int    021
  275.  
  276. Exit:
  277.         Mov     SP,Stackx               ;Insure exiting stack
  278.  
  279. Done:        Mov     AL,Errlvl               ;Return to system
  280.       Mov    AH,04C                  ; via EXIT
  281.       Int    021
  282. ;
  283. ;
  284. ;     Start at Root Directory
  285.  
  286. No_Arg:
  287.                                      ; If no argument then set current
  288.       Mov    AH,CHDIR                ; path to root directory
  289.       Mov    DX,Offset RootDir
  290.       Int    021
  291.  
  292.       Ret
  293.  
  294. ;
  295. ;
  296. ;     Search for matching sub directory
  297.  
  298. GetDir:
  299.  
  300.           Mov     Done_Flag,0
  301.  
  302.  
  303. ;     Find first or next subdirectory level
  304. ;     -------------------------------------
  305.  
  306. NextLevel:
  307.           Mov     DX,[DTAPointer]      ; Next nested DTA
  308.           Mov     AH,1Ah               ; For DOS call to set DTA
  309.           Int     21h                  ; Do it
  310.  
  311.           Cmp     [Direction],0        ; Check if we're nesting
  312.           Jnz     FindNextFile         ; If not, we're continuing
  313.  
  314.           Mov     DX,Offset SearchAsciiZ     ; We search for *.*
  315.           Mov     CX,10h               ; Subdirectory attribute
  316.           Mov     AH,4Eh               ; Find first file
  317.           Int     21h                  ;   by calling DOS
  318.  
  319.           Jmp     Short TestMatch      ; Hop around next section
  320. FindNextFile:
  321.           Mov     AH,4Fh               ; Find next file
  322.           Int     21h                  ;   by calling DOS
  323. TestMatch:
  324.           Jc      NoMoreFiles          ; If CY flag, at end of rope
  325.  
  326.           Mov     BX,[DTAPointer]      ; Our find stuff is here
  327.           Test    B [BX + 21],10h      ; Test if directory attribute
  328.           Jz      FindNextFile         ; If not, continue search
  329.  
  330.           Add     BX,30                ; Now points to directory name
  331.           Cmp     Byte Ptr [BX],'.'    ; Ignore "." and ".." entries
  332.           Jz      FindNextFile         ;   by continuing the search
  333.  
  334.           Cmp     OneDeepFlag,1        ; looking only at this dir?
  335.           Je      Compare
  336.  
  337.           Push    BX                   ; save pointer to subdir name
  338.  
  339.           Mov     DX,BX                ; Now DX points to found dir
  340.           Mov     AH,3Bh               ; Set up DOS function call
  341.           Int     21h                  ; And change directory
  342.  
  343.           Pop     BX                   ; get pointer to subdir name back
  344.     Compare:
  345.           Push    CX                   ;check to see if its the path we want
  346.           Push    SI
  347.           Push    DI
  348.           Sub     CX,CX
  349.           Mov     CX,Count
  350.           Mov     DI, Offset Sub_Dir
  351.           Lea     SI, BX
  352.           Repe    Cmpsb
  353.  
  354.           Pop     DI
  355.           Pop     SI
  356.           Pop     CX
  357.  
  358.           Jz      Found                ; matched up so leave
  359.  
  360.           Cmp     OneDeepFlag,1
  361.           Jne     GoOn
  362.           Mov     [Direction],-1
  363.           Jmp Short NextLevel
  364.       GoOn:
  365.           Add     [DtaPointer],43      ; New DTA for new level
  366.           Mov     [Direction],0        ; I.E., Find first file
  367.  
  368.           Jmp     NextLevel            ; All ready to cycle through
  369.  
  370. ;     No More Files Found -- go back to previous level
  371. ;     ------------------------------------------------
  372.  
  373. NoMoreFiles:
  374.           Cmp     [DTAPointer],Offset DtaAreaBegin   ; See if back at start
  375.  
  376.           Jz      ExitGD               ; If so, that's all, folks
  377.  
  378.           Sub     [DTAPointer],43      ; Back one for previous
  379.           Mov     [Direction],-1       ; I.E., will find next file
  380.  
  381.           Mov     DX,Offset BackOneDir ; The string ".."
  382.           Mov     AH,3Bh               ; Call to change directory
  383.           Int     21h                  ; Change directory to father
  384.  
  385.           Jmp     NextLevel            ; And continue the search
  386. Found:
  387.           Cmp     OneDeepFlag,1
  388.           If ne Jmp Short F1
  389.  
  390.           Mov     DX,BX                ; Now DX points to found dir
  391.           Mov     AH,3Bh               ; Set up DOS function call
  392.           Int     21h                  ; And change directory
  393.       F1:
  394.           Mov     Done_Flag,1
  395. ExitGD:
  396.           Ret
  397.  
  398. ;
  399. ;
  400. ;     Print String like INT 21H function 9
  401.  
  402. PrintS:                                      ; DX has offset to string
  403.       Push   SI                      ;  ending in char x'FF'
  404.       Push   BX
  405.       Push   CX
  406.       Mov    SI,DX                   ; Ptr to string text
  407.       Sub    CX,CX                   ; Overall text length
  408. PS1:  Lodsb
  409.       Cmp    AL,Stopper              ; Ending hex FF?
  410.       Je     PS9
  411.       Inc    CX
  412.       Jmp    Short PS1
  413.  
  414. PS9:
  415.       Mov    BX,1                    ; Standard output device
  416.       Mov    AH,40h                  ;  to write to
  417.       Int    21h
  418.  
  419.       Pop    CX                      ; Recover registers
  420.       Pop    BX
  421.       Pop    SI
  422.       Ret
  423.  
  424.  
  425. DtaAreaBegin   equ     $                          ;   is also the DTA area
  426. DtaAreaEnd     equ     DtaAreaBegin + 32 * 43     ; Can have 32 DTAs of 43 bytes
  427.